/**
 * Created by Administrator on 2016/1/19.
 */
define("wangwu/sxyc/Handle",
    [
        'dojo/_base/declare',
        'dojo/_base/lang',
        'dojo/on',
        'dojo/dom-construct',
        'dojo/query',
        "dojo/_base/event",
        "dojo/_base/array",

        'esri/Evented',
        'esri/geometry/Point',
        "esri/graphic",
        'esri/toolbars/edit',
        "esri/toolbars/draw",
        'esri/layers/GraphicsLayer',
        "esri/dijit/editing/TemplatePicker",

        "wangwu/sxyc/getData"

    ],
    function(declare,lang, on,domConstruct,djQuery,event,arrayUtils,
             Evented,Point,Graphic,Edit,Draw,GraphicsLayer,TemplatePicker,getData){
        var d = declare([Evented],{
            declaredClass:'wangwu.sxyc.Handle',
            _eventMap:{
                'edit-end':['data']
            },
            constructor:function(map,o){
                this.map = map;
                this.option={
                    layerid01:"干渠",
                    layerid02:"支渠"
                };
                (o)?lang.mixin(this.option, o):0;
                this.mainLayer = this.map.getLayer(this.option.layerid01);
                this.branchLayer = this.map.getLayer(this.option.layerid02);
                this.initialized = false;
                //调试用
                this.buttonId = 'testDojo-'+parseInt(Math.random()*1000);
                domConstruct.create('div', {
                    'id':'aaaa1111',
                    'style':'position:absolute;right:20px;top:50px;width:100px',
                    'innerHTML':'<input type="button" value="开始编辑" id="'+this.buttonId+'"/>'
                }, map.id);
                var button = djQuery('#'+this.buttonId);
                on(button, 'click', lang.hitch(this, this.init));
                /*this.buttonId01 = 'testDojo-'+parseInt(Math.random()*1000);
                domConstruct.create('div', {
                    'id':'aaaa1111',
                    'style':'position:absolute;right:20px;top:100px;width:100px',
                    'innerHTML':'<input type="button" value="编辑" id="'+this.buttonId+'"/>'
                }, map.id);
                var button01 = djQuery('#'+this.buttonId01);
                on(button01, 'click', lang.hitch(this, this.save));*/
            },
            //-----------------------public----------------------------------

            init:function(){
                if (this.initialized) return;
                this.initialized = true;
                //this.edit();
                this.add();
            },
            setDataSource:function(o){
                (o)?lang.mixin(this.option, o):0;
            },
            edit:function(){
                this._edit();
            },
            add:function(){
                this._add();
            },
            /*save:function(e){
                this.edit.on("deactivate",lang.hitch(this,this._save));
                this.edit.deactivate();
            },*/
            //-----------------------private----------------------------------
            _init:function(){

            },
            _edit:function(){
                var selectGraphic;
                var currentLayer = null;
                var layer = this.map.getLayer(this.option.layerid);
                console.log("开始编辑", layer);

                /*var layers = arrayUtils.map(evt.layers, function(result) {
                    return result.layer;
                });
                arrayUtils.forEach(layers, function(layer) {

                });*/

                var editToolbar = new Edit(this.map);
                editToolbar.on("deactivate", function(evt) {
                    console.log(evt.graphic);
                    //layer.remove(selectGraphic);
                    //layer.add(evt.graphic);
                    //layer.redraw();
                    console.log(layer);
                    this.get = new getData(this.map,{layerid:layer.id});
                    var data = this.get.getData();

                    this.onEditEnd(data);

                });

                var editingEnabled = false;
                layer.on("dbl-click", function(evt) {
                    event.stop(evt);
                    if (editingEnabled === false) {
                        editingEnabled = true;
                        editToolbar.activate(Edit.EDIT_VERTICES , evt.graphic);
                        selectGraphic = evt.graphic;
                    } else {
                        currentLayer = this;
                        editToolbar.deactivate();
                        editingEnabled = false;
                    }
                });

                layer.on("click", function(evt) {
                    event.stop(evt);
                    if (evt.ctrlKey === true || evt.metaKey === true) {  //delete feature if ctrl key is depressed
                        layer.remove(evt.graphic);
                        currentLayer = this;
                        editToolbar.deactivate();
                        editingEnabled=false;
                    }
                });
            },
            _add:function(){
                var layer = this.map.getLayer(this.option.layerid);
                //console.log("开始添加", layer);
                var drawToolbar = new Draw(this.map);
                drawToolbar.activate(Draw.POLYLINE);
                var self = this;
                drawToolbar.on("draw-end", function(evt) {
                    drawToolbar.deactivate();
                    var ww = [];
                    for(var i=0; i<layer.graphics.length;i++){
                        var ID = layer.graphics[i].attributes["OBJECTID"];
                        ww.push(ID);
                    }
                    var OBJECTID = Math.max.apply(Math, ww);
                    //console.log(OBJECTID);
                    var attr = {
                        "CODECAN1":"",
                        "ID":"1111111",
                        "OBJECTID":OBJECTID+1
                    }
                    var newGraphic = new Graphic(evt.geometry, layer.graphics[0].symbol, attr);
                    layer.add(newGraphic);

                    var ww = new getData(self.map,{layerid:layer.id});
                    var result = ww.getData();
                    self.send(result);
                });
            },
            //----------------------------------------------------------------------
            onEditEnd:function(data){

            },
            send:function(data){
                $.ajax({
                    url:'/sendmsg',
                    type:'post',
                    data:{
                        data:JSON.stringify(data)
                    },
                    success:function(){

                    },
                    error:function(){

                    }
                });
            }
        });
        return d;

    })